package com.wash.shoes.controller.mobile;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.vo.OrderCommentVo;
import com.wash.shoes.entity.OrderCommentPo;
import com.wash.shoes.entity.OrderGoodPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.OrderCommentService;
import com.wash.shoes.service.OrderGoodService;
import com.wash.shoes.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ljg
 * @since 2024-12-03
 */
@Api(tags = {"移动端-订单的评价"})
@RestController
@RequestMapping("/mobile-api/orderComment")
public class OrderCommentMController {

    @Autowired
    private OrderCommentService orderCommentService;

    @Autowired
    OrderGoodService orderGoodService;

    @Autowired
    OrderService orderService;


    @ApiOperation("订单的评价列表")
    @PostMapping("/list")
    public R<Page<OrderCommentVo>> pageList(@RequestBody OrderCommentPo orderCommentPo) {
        Page<OrderCommentVo> page = orderCommentService.pageList(orderCommentPo);
        return R.ok(page);
    }


    @ApiOperation("订单评价")
    @PostMapping("/add")
    @Transactional
    public R<OrderCommentPo> add(@RequestBody OrderCommentPo orderCommentPo) {

        OrderCommentPo orderCommentPoDb = orderCommentService.getByOrderId(orderCommentPo.getOrderId());
        if (orderCommentPoDb != null) {
            throw new WashException("该订单已经评价");
        }
        List<OrderGoodPo> orderGoodPoList = orderGoodService.getOrderGoods(orderCommentPo.getOrderId());
        // 评论鞋子
        orderCommentPo.setGoodsId(orderGoodPoList.get(0).getGoodsId());
        orderCommentPo.setOrderId(orderCommentPo.getOrderId());
        orderCommentService.save(orderCommentPo);

        // 订单的状态
        OrderPo orderPo = orderService.getById(orderCommentPo.getOrderId());
        orderPo.setXcxStatus(7);
        orderService.updateById(orderPo);

        return R.ok(orderCommentPo);
    }


}

